package com.ming.seatMonitoring.util;

import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 小明
 * @date 2021/10/25
 * @description
 */
public class ExcelUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtil.class);

    public static List<Object> excelToShopIdList(InputStream inputStream) {
        List<Object> list = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
//获取第一个工作表对象
            Sheet sheet = workbook.getSheetAt(0);
//总行数
            int rowLength = sheet.getLastRowNum()+1;
            System.out.println("总行数有多少行:"+rowLength);
//获取第一行的总列数
            Row row = sheet.getRow(0);
            int colLength = row.getLastCellNum();
            System.out.println("总列数有多少列:"+colLength);
//得到指定的单元格
//从(0,0)遍历到(rowLength-1,colLength-1)
            for (int i = 0; i < rowLength; i++) {
                 row = sheet.getRow(i);
                for (int j = 0; j < colLength; j++) {
                    Cell cell = row.getCell(j);
                    if (cell != null){
//                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String data = cell.getStringCellValue();
                        //修剪前后空白
                        data = data.trim();
                        list.add(data);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("parse excel file error :", e);
        }
        return list ;
    }
}

